

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pyamp.web.http.server &mdash; pyamp v1.2 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="top" title="pyamp v1.2 documentation" href="../../../../index.html" />
    <link rel="up" title="Module code" href="../../../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for pyamp.web.http.server</h1><div class="highlight"><pre>
<span class="c"># Copyright 2012 Brett Ponsler</span>
<span class="c"># This file is part of pyamp.</span>
<span class="c">#</span>
<span class="c"># pyamp is free software: you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU General Public License as published by</span>
<span class="c"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c"># (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># pyamp is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c"># GNU General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU General Public License</span>
<span class="c"># along with pyamp.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="sd">&#39;&#39;&#39;The server module contains various classes pertaining to</span>
<span class="sd">creating HTTP servers.</span>

<span class="sd">&#39;&#39;&#39;</span>
<span class="kn">import</span> <span class="nn">httplib</span>
<span class="kn">from</span> <span class="nn">BaseHTTPServer</span> <span class="kn">import</span> <span class="n">HTTPServer</span>

<span class="kn">from</span> <span class="nn">pyamp.processes</span> <span class="kn">import</span> <span class="n">Thread</span>
<span class="kn">from</span> <span class="nn">pyamp.exceptions</span> <span class="kn">import</span> <span class="n">UndefinedFunctionError</span>


<div class="viewcode-block" id="HttpServer"><a class="viewcode-back" href="../../../../web.html#pyamp.web.http.server.HttpServer">[docs]</a><span class="k">class</span> <span class="nc">HttpServer</span><span class="p">(</span><span class="n">HTTPServer</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;Create a basic HTTP server.</span>

<span class="sd">    Example::</span>

<span class="sd">        from BaseHTTPServer import BaseHTTPRequestHandler</span>


<span class="sd">        class RequestHandler(BaseHTTPRequestHandler):</span>
<span class="sd">            def do_GET(self):</span>
<span class="sd">                &#39;&#39;&#39;</span> <span class="s">&#39;&#39;&#39;</span>
<span class="s">                print &quot;GET:&quot;, self.path</span>

<span class="s">        server = HttpServer(12345, RequestHandler)</span>
<span class="s">        server.serve_forever()</span>

<span class="s">    &#39;&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">requestHandlerClass</span><span class="p">,</span> <span class="n">host</span><span class="o">=</span><span class="s">&#39;localhost&#39;</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * port -- The port for the server</span>
<span class="sd">        * requestHandlerClass -- The class used to handle requests</span>
<span class="sd">        * host -- The host name for the server</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">HTTPServer</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="p">(</span><span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">),</span> <span class="n">requestHandlerClass</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="HttpServerThread"><a class="viewcode-back" href="../../../../web.html#pyamp.web.http.server.HttpServerThread">[docs]</a><span class="k">class</span> <span class="nc">HttpServerThread</span><span class="p">(</span><span class="n">Thread</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;The HttpServerThread class provides an implementation of an</span>
<span class="sd">    HttpServer that runs in its own thread.</span>

<span class="sd">    Example::</span>

<span class="sd">        from time import sleep</span>

<span class="sd">        from pyamp.web.http.requests import StoppableRequestHandler</span>


<span class="sd">        server = HttpServerThread(12345, StoppableRequestHandler)</span>
<span class="sd">        server.start()</span>

<span class="sd">        while True:</span>
<span class="sd">            try:</span>
<span class="sd">                sleep(0.1)</span>
<span class="sd">            except (KeyboardInterrupt, SystemExit):</span>
<span class="sd">                print &quot;Stopping server!&quot;</span>
<span class="sd">                server.shutdown()</span>
<span class="sd">                break</span>

<span class="sd">        print &quot;Done.&quot;</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">requestHandlerClass</span><span class="p">,</span> <span class="n">host</span><span class="o">=</span><span class="s">&#39;localhost&#39;</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        * port -- The port for the server</span>
<span class="sd">        * requestHandlerClass -- The class used to handle requests</span>
<span class="sd">        * host -- The host name for the server</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="c"># Ensure that the request handler can handle the quit action</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">requestHandlerClass</span><span class="p">,</span> <span class="s">&quot;do_QUIT&quot;</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">UndefinedFunctionError</span><span class="p">(</span><span class="n">requestHandlerClass</span><span class="p">,</span> <span class="s">&quot;do_QUIT&quot;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">__host</span> <span class="o">=</span> <span class="n">host</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__port</span> <span class="o">=</span> <span class="n">port</span>

        <span class="n">Thread</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__server</span> <span class="o">=</span> <span class="n">HttpServer</span><span class="p">(</span><span class="n">port</span><span class="p">,</span> <span class="n">requestHandlerClass</span><span class="p">,</span> <span class="n">host</span><span class="p">)</span>

<div class="viewcode-block" id="HttpServerThread.cycle"><a class="viewcode-back" href="../../../../web.html#pyamp.web.http.server.HttpServerThread.cycle">[docs]</a>    <span class="k">def</span> <span class="nf">cycle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_i</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Called once during every cycle of the thread.</span>

<span class="sd">        * _i -- The current cycle number</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__server</span><span class="o">.</span><span class="n">handle_request</span><span class="p">()</span>
</div>
<div class="viewcode-block" id="HttpServerThread.onShutdown"><a class="viewcode-back" href="../../../../web.html#pyamp.web.http.server.HttpServerThread.onShutdown">[docs]</a>    <span class="k">def</span> <span class="nf">onShutdown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Called in the event that the Thread is shut down.&#39;&#39;&#39;</span>
        <span class="c"># Send a QUIT message to the server to indicate that it should stop</span>
        <span class="n">conn</span> <span class="o">=</span> <span class="n">httplib</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">:</span><span class="si">%d</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__host</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__port</span><span class="p">))</span>
        <span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s">&quot;QUIT&quot;</span><span class="p">,</span> <span class="s">&quot;/&quot;</span><span class="p">)</span>
        <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span>
</div>
<div class="viewcode-block" id="HttpServerThread.onStop"><a class="viewcode-back" href="../../../../web.html#pyamp.web.http.server.HttpServerThread.onStop">[docs]</a>    <span class="k">def</span> <span class="nf">onStop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Called in the event that the Thread is stopped.&#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__server</span><span class="o">.</span><span class="n">socket</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

</div></div>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">pyamp.web.http</span> <span class="kn">import</span> <span class="n">BaseRequestHandler</span><span class="p">,</span> <span class="n">StatusCodes</span><span class="p">,</span> <span class="n">HtmlPage</span><span class="p">,</span> \
        <span class="n">JsonPage</span>

    <span class="k">class</span> <span class="nc">NotFoundPage</span><span class="p">(</span><span class="n">HtmlPage</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Test not found page.&#39;&#39;&#39;</span>

        <span class="k">def</span> <span class="nf">_getContent</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">_arguments</span><span class="p">):</span>
            <span class="sd">&#39;&#39;&#39;Returnt test content.&#39;&#39;&#39;</span>
            <span class="k">return</span> <span class="s">&quot;I&#39;m sorry, but we could not find: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">path</span>

    <span class="k">class</span> <span class="nc">HtmlExample</span><span class="p">(</span><span class="n">HtmlPage</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Test HTML page.&#39;&#39;&#39;</span>

        <span class="k">def</span> <span class="nf">_getContent</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">arguments</span><span class="p">):</span>
            <span class="sd">&#39;&#39;&#39;Return test content.&#39;&#39;&#39;</span>
            <span class="n">name</span> <span class="o">=</span> <span class="n">arguments</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;name&quot;</span><span class="p">,</span> <span class="s">&quot;ANONYMOUS&quot;</span><span class="p">)</span>
            <span class="n">lines</span> <span class="o">=</span> <span class="p">[</span>
                <span class="s">&quot;Hello, </span><span class="si">%s</span><span class="s">, welcome to my &lt;b&gt;HTML&lt;/b&gt; page!&quot;</span> <span class="o">%</span> <span class="n">name</span><span class="p">,</span>
                <span class="s">&quot;&lt;br&gt;(Set your name with the &#39;name&#39; argument)&quot;</span><span class="p">,</span>
                     <span class="p">]</span>
            <span class="k">return</span> <span class="s">&quot;&lt;br&gt;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span>

    <span class="k">class</span> <span class="nc">JsonExample</span><span class="p">(</span><span class="n">JsonPage</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Test HTML page.&#39;&#39;&#39;</span>

        <span class="k">def</span> <span class="nf">_getContent</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">_arguments</span><span class="p">):</span>
            <span class="sd">&#39;&#39;&#39;Return test content.&#39;&#39;&#39;</span>
            <span class="k">return</span> <span class="p">{</span><span class="s">&quot;key1&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
                    <span class="s">&quot;key2&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&quot;key3&quot;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
                             <span class="s">&quot;key4&quot;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]},</span>
                    <span class="s">&quot;key5&quot;</span><span class="p">:</span> <span class="s">&quot;value6&quot;</span><span class="p">}</span>

    <span class="k">class</span> <span class="nc">ExampleRequestHandler</span><span class="p">(</span><span class="n">BaseRequestHandler</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Test request handler.&#39;&#39;&#39;</span>
        <span class="n">pages</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;/index.html&quot;</span><span class="p">:</span> <span class="n">HtmlExample</span><span class="p">(),</span>
                 <span class="s">&quot;/json.html&quot;</span><span class="p">:</span> <span class="n">JsonExample</span><span class="p">()}</span>

        <span class="n">errorPages</span> <span class="o">=</span> <span class="p">{</span><span class="n">StatusCodes</span><span class="o">.</span><span class="n">FileNotFound</span><span class="p">:</span> <span class="n">NotFoundPage</span><span class="p">()}</span>

    <span class="n">server</span> <span class="o">=</span> <span class="n">HttpServer</span><span class="p">(</span><span class="mi">12345</span><span class="p">,</span> <span class="n">ExampleRequestHandler</span><span class="p">)</span>
    <span class="n">server</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../../../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="../../../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Brett Ponsler.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>